const {
  releaseArticleService,
  saveArticleService,
  cancelSaveArticleService,
  supportArticleService,
  cancelSupportArticleService,
  commentArticleService,
  getArticleService,
  getArticleByIdService,
  getHotArticleService,
  getRandomArticleService,
} = require("../service/article.service");
const {
  getMissingKeys,
  releaseArticle_Parameter,
  article_Parameter,
  paging_Parameter,
} = require("../config/parameter");
const { PARAMETER_MISSING } = require("../config/error-constants");

class articleController {
  // 发布文章
  async releaseArticle(ctx, next) {
    const articleParameter = {};
    articleParameter.uid = ctx.userInfo.id;
    // 100-500随机一个浏览量
    articleParameter.view_counts = Math.floor(Math.random() * 500) + 100;
    const requestParameter = ctx.request.body;
    Object.assign(articleParameter, requestParameter);

    const verifyParameter = getMissingKeys(
      releaseArticle_Parameter,
      requestParameter
    );
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await releaseArticleService(articleParameter);

    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `发布活动成功~`,
      data: result,
    };
  }

  // 收藏文章
  async saveArticle(ctx, next) {
    const parameter = {};
    parameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;
    Object.assign(parameter, requestParameter);

    const verifyParameter = getMissingKeys(article_Parameter, requestParameter);
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await saveArticleService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `收藏成功~`,
      data: result,
    };
  }

  // 取消收藏
  async cancelSaveArticle(ctx, next) {
    const parameter = {};
    parameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;
    Object.assign(parameter, requestParameter);

    const verifyParameter = getMissingKeys(article_Parameter, requestParameter);
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await cancelSaveArticleService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `取消收藏成功~`,
      data: result,
    };
  }

  // 点赞文章
  async supportArticle(ctx, next) {
    const parameter = {};
    parameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;
    Object.assign(parameter, requestParameter);

    const verifyParameter = getMissingKeys(article_Parameter, requestParameter);
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await supportArticleService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `点赞成功~`,
      data: result,
    };
  }

  // 取消点赞
  async cancelSupportArticle(ctx, next) {
    const parameter = {};
    parameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;
    Object.assign(parameter, requestParameter);

    const verifyParameter = getMissingKeys(article_Parameter, requestParameter);
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await cancelSupportArticleService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `取消点赞成功~`,
      data: result,
    };
  }

  // 评论文章
  async commentArticle(ctx, next) {
    const user_id = ctx.userInfo.id;

    const { content } = ctx.request.body;
    if (!content) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "content");
    }
    let comment_id = ctx.query.comment_id;
    if (!comment_id) {
      comment_id = null;
    }
    const article_id = ctx.query.article_id;
    if (!article_id) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "article_id");
    }

    const result = await commentArticleService({
      user_id,
      content,
      article_id,
      comment_id,
    });

    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `评论文章成功~`,
      data: result,
    };
  }

  // 获取文章
  async getArticle(ctx, next) {
    const parameter = {
      ...ctx.query,
    };

    const verifyParameter = getMissingKeys(paging_Parameter, parameter);
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await getArticleService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取文章成功~`,
      data: result,
    };
  }

  // 通过Id获取文章
  async getArticleById(ctx, next) {
    const { articleId } = ctx.params;

    if (!articleId) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "articleId");
    }

    const result = await getArticleByIdService(articleId);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取文章成功~`,
      data: result,
    };
  }

  // 获取热门文章
  async getHotArticle(ctx, next) {
    const limit = ctx.query.limit;

    if (!limit) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "limit");
    }

    const result = await getHotArticleService(limit);

    console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取热门文章成功~`,
      data: result,
    };
  }

  // 获取随机文章
  async getRandomArticle(ctx, next) {
    const limit = ctx.query.limit;

    if (!limit) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "limit");
    }

    const result = await getRandomArticleService(limit);

    console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取随机文章成功~`,
      data: result,
    };
  }
}

module.exports = new articleController();
